#include <tsin/util/MemCheck.hpp>

#include <cstdio>
#include <cstdlib>
#include <cstddef>
#include <cassert>

using std::size_t;

namespace tsin {
        namespace memCheck {
                UTIL_DLLEXPORT int findPtr(void *p);
                UTIL_DLLEXPORT void delPtr(void *p);
        }
}

using tsin::memCheck::findPtr;
using tsin::memCheck::delPtr;

void operator delete(void *p)
{
        if (findPtr(p) >= 0) {
                free(p);
                delPtr(p);
                if (traceFlag)
                        printf("Deleted memory at address %p\n", p);
        } else if (!p && traceFlag) {
                printf("Attempt to delete unknown pointer: %p\n", p);
        }
}

void operator delete[](void *p)
{
        operator delete(p);
}

void operator delete(void *p, const char *, long)
{
        operator delete(p);
}

void operator delete[](void *p, const char *, long)
{
        operator delete[](p);
}
